//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------

package reader.utils
{
	import __AS3__.vec.Vector;
	
	import reader.core.ReaderResource;
	import reader.model.vo.BlogLabelVO;
	import reader.model.vo.BlogSourceVO;
	import reader.model.vo.OPMLItemVO;
	
	public class OPMLUtils
	{
		public static const INPORT_OPML:String = "inportOpml";
		public static const OUTPORT_OPML:String = "outportOpml";
		
		public static const XML_VERSION:String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		
		private static const TYPE_RSS:String = "rss";
		
		static public function outportOPML(source:Vector.<BlogSourceVO>, labels:Vector.<BlogLabelVO>, username:String):XML
		{
			var opml:XML = <opml version="1.0"></opml>;
			
			var head:XML = <head></head>;
			head.title = String(ReaderResource.instance.resource.opmlHeadTitle).replace(/\?/, username);
			opml.appendChild(head);
			
			var body:XML = <body></body>;
			
			for each(var bl:BlogLabelVO in labels)
			{
				var folderXml:XML = <outline title={bl.name} text={bl.name} labelid={bl.labelid}/>;
				body.appendChild(folderXml);
			}
			
			var noLabel:Vector.<XML> = new Vector.<XML>();
			for each(var bs:BlogSourceVO in source)
			{
				var itemXml:XML = <outline text={bs.blogName} title={bs.blogName} xmlUrl={bs.blogRSS} htmlUrl={bs.blogLink} type="rss" />;
				
				if(bs.labelId != -1)
				{
					var foldList:XMLList = body.outline.(@labelid == bs.labelId);
					foldList[0].appendChild(itemXml);
				}
				else
				{
					noLabel.push(itemXml);
				}
			}
			for each(var xml:XML in noLabel)
			{
				body.appendChild(xml);
			}
			noLabel = null;
			for each(var xml:XML in body..outline)
			{
				if(String(xml.@labelid) != "")
				{
					delete xml.@labelid;
				}
			}	
			opml.appendChild(body);
			return opml;
		}
		
		static public function parseOPML(source:XML):Vector.<OPMLItemVO>
		{
			var list:Vector.<OPMLItemVO> = new Vector.<OPMLItemVO>();
			var rootOPML:OPMLItemVO = new OPMLItemVO();
			for each(var xml:XML in source.body.outline)
			{
				if(String(xml.@type) != TYPE_RSS)
				{
					var bl:BlogLabelVO = new BlogLabelVO();
					bl.name = xml.@title;
					var item:OPMLItemVO = new OPMLItemVO(bl);
					for each(var outline:XML in xml.outline)
					{
						if(String(outline.@type) == TYPE_RSS)
						{
							var bs:BlogSourceVO = new BlogSourceVO();
							bs.blogName = outline.@title;
							bs.blogRSS = outline.@xmlUrl;
							bs.blogLink = outline.@htmlUrl;
							bs.lastPubDate = BlogSourceVO.createLastPubDate();
							item.subscribe.push(bs);
						}
						
					}
					if(item.subscribe.length > 0)
						list.push(item);
				}
				else if(String(xml.@type) == TYPE_RSS)
				{
					var bs:BlogSourceVO = new BlogSourceVO();
					bs.blogName = xml.@title;
					bs.blogRSS = xml.@xmlUrl;
					bs.blogLink = xml.@htmlUrl;
					bs.lastPubDate = new Date();
					rootOPML.subscribe.push(bs);
				}
			}
			if(rootOPML.subscribe.length > 0)
				list.push(rootOPML);
			return list;
		}

	}
}